package com.ch14;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 用户登录界面
 */
public class Login extends JFrame {

    // JLable : lbl
    // JButton: btn
    // JPanel : pnl
    // JTextField : txt
    // JCheckBox: chk
    // JRadioButton: rdb
    // JTextArea : txt
    // JComboBox     : cmb
    //
    JLabel lblGrade, lblUserName,lblPassword;
    JTextField txtUserName;
    JPasswordField txtPassword;
    JButton btnLogin, btnCancel;
    JCheckBox chkRemember;
    JPanel pnlContainer;
    JComboBox cmbGrade;
    public  Login(){
        initComponents();
    }
    void initComponents(){
        this.setTitle("用户登录");
        // 布局规划：panel使用绝对布局，Frame使用默认布局
        pnlContainer = new JPanel();
        pnlContainer.setLayout(null);

        lblGrade = new JLabel();
        lblGrade.setText("年级：");
        lblGrade.setBounds(80,40,80,20);

        cmbGrade = new JComboBox();
        cmbGrade.addItem("--年级--");
        cmbGrade.addItem("2021");
        cmbGrade.addItem("2022");
        cmbGrade.addItem("2023");
        cmbGrade.setBounds(180,40,80,20);

        pnlContainer.add(lblGrade);
        pnlContainer.add(cmbGrade);

        lblUserName = new JLabel();
        lblUserName.setText("用户名：");
        lblUserName.setBounds(80,70,80,20);
        lblPassword = new JLabel();
        lblPassword.setText("密码：");
        lblPassword.setBounds(80,100,80,20);

        pnlContainer.add(lblUserName);
        pnlContainer.add(lblPassword);

        txtUserName = new JTextField();
        txtUserName.setBounds(180,70,120,20);
        txtPassword = new JPasswordField();
        txtPassword.setEchoChar('*');
        txtPassword.setBounds(180,100,120,20);

        pnlContainer.add(txtUserName);
        pnlContainer.add(txtPassword);

        btnLogin= new JButton();
        btnLogin.setText("登录");
        btnLogin.setBounds(80,150,80,25);
        btnLogin.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(txtUserName.getText()!=null && txtUserName.getText().length()>0){
                            Home home = new Home(txtUserName.getText());
                            // 针对Home对象进行一些别的初始化
                            home.setVisible(true);
                            
                        }else{
                            JOptionPane.showConfirmDialog(btnLogin,"用户名不能为空");
                        }
                    }
                }
        );

        btnCancel = new JButton();
        btnCancel.setText("取消");
        btnCancel.setBounds(200,150,80,25);

        pnlContainer.add(btnLogin);
        pnlContainer.add(btnCancel);

        chkRemember= new JCheckBox();
        chkRemember.setText("记住我");
        chkRemember.setBounds(150,180,80,25);

        pnlContainer.add(chkRemember);

        btnCancel.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane.showConfirmDialog(pnlContainer,"你确定要取消吗?");
                    }
                }
        );

        this.getContentPane().add(pnlContainer);

        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        this.setBounds(800,600,350,260);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        Login login = new Login();
    }
}
